import axios from 'axios'
import pinia from '@/store/index.js'
import token from '@/store/token.js'

const tokenStore = token(pinia)

const baseURL = import.meta.env.VITE_APP_FILE_API
// 必须携带token
const req = axios.create({
    baseURL : baseURL,
    timeout : 10000,
})

req.interceptors.request.use(
    config => {
        let tokenStr = tokenStore.getValidToken();
        if(tokenStr.length > 0){
            config.headers['W-Token'] = tokenStr 
        }
        return config
    },
    error => {
        console.warn('req error:',error);
        return Promise.reject(error)
    }
)

req.interceptors.response.use(
    response => {
        if(response.status != 200){
            return Promise.reject(new Error(response.statusText || 'Error'))
        }
        return response.data
    },
    error => {
        console.log('req response error:' + error)
    }
)

const fileApi = {
   
    baseURL : baseURL, 

    uploadOneURL : baseURL + '/file/upload', 

    // 上传
    uploadOne : (file)=>{
        return req.post(fileApi.uploadOneURL, {
            file,
        })
    },
    
}



export default fileApi